package com.medicine.sys.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medicine.sys.entity.Department;
import com.medicine.sys.entity.UserDepartment;
import com.medicine.sys.mapper.UserDepartmentMapper;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;

@Service
public class UserDepartmentService extends ServiceImpl<UserDepartmentMapper, UserDepartment> {


    public void install(UserDepartment userDepartment) throws InvocationTargetException, IllegalAccessException {
        UserDepartment userDepartmentOne = getUserId(userDepartment.getUserId());
        if(userDepartmentOne !=null){
            userDepartmentOne.setDepartmentId(userDepartment.getDepartmentId());
        }else{
            userDepartmentOne = new UserDepartment();
            userDepartmentOne.setDepartmentId(userDepartment.getDepartmentId());
            userDepartmentOne.setUserId(userDepartment.getUserId());
        }
        saveOrUpdate(userDepartmentOne);
    }

    public UserDepartment getUserId(String userId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        return getOne(queryWrapper);
    }

    public Department getOneUserId(String userId){
        return this.baseMapper.getOneUserId(userId);
    }


}
